﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Serialization;
using IFWiki.Utils;

namespace IFWiki.Models
{
    public partial class History : IRevisable
    {
        public string Reason { get; set; }
        public DateTime RevisedAt { get; set; }
        public string RevisedBy { get; set; }

        public string Name { get; set; }
        public string InnerType { get; set; }
        
        public bool IsDiscussion { get; private set; }

        [XmlIgnore()]
        public List<IRevisable> Revisions { get; private set; }


        public List<IRevisable> LoadRevisions()
        {
            return this.Revisions;
        }

        public void Save(string reason, string revisedBy)
        {
            throw new Exception("You can't save history. History is therefore doomed.");
        }
        public void Revert(string reason, string name, string revertedBy)
        {
            throw new Exception("You can't revert history.");
        }

    }
    public partial class History
    {
        public History(Type innerType, string name)
        {
            this.InnerType = innerType.Name;
            this.Name = name;

            var item = (IRevisable)Persister.Load(this.Name, innerType)?? Defaults.GetArticle(innerType, name);
            Debug.Assert(item != null);

            this.Revisions = item.LoadRevisions();
            this.Reason = item.Reason;
            this.RevisedAt = item.RevisedAt;
            this.IsDiscussion = item.IsDiscussion;
        }
    }
}
